package com.ChessUtils;

import com.beans.Chess;

import java.util.List;

/**
 * @author acoffee
 * @create 2021-09-05 21:19
 */
public class JugWinLoseUtil {

    // 换边
    public static char ChangeSides(char playSides) {
        return playSides = playSides == 'B' ? 'W' : 'B';
    }

    //判断输赢
    public static boolean WinGame(Chess chess, int chessCount, List<Chess> chesses) {
        int x = chess.getX();
        int y = chess.getY();

        //记录一条线上棋子的数量
        int WinCount = 1;

        boolean level = Level(x, y, chessCount, chesses, WinCount, chess);
        boolean upright = Upright(x, y, chessCount, chesses, WinCount, chess);
        boolean lfetAngle = LfetAngle(x, y, chessCount, chesses, WinCount, chess);
        boolean rightAngle = rightAngle(x, y, chessCount, chesses, WinCount, chess);

        return (level || upright || lfetAngle || rightAngle);
    }

    // 判断落子的合法性
    public static boolean ChessLegal(int x, int y, int PaneLength, int SceneLength, int chessCount, List<Chess> chesses) {

        //看棋子是否在棋盘的规定范围并且此处是否存在棋子
        return ((x + 1) * PaneLength >= PaneLength && x * PaneLength < SceneLength - PaneLength && (y + 1) * PaneLength >= PaneLength
                && y * PaneLength < SceneLength - PaneLength && !ExistChess(x, y, chessCount, chesses));
    }

    // 看该坐标是否存在棋子
    private static boolean ExistChess(int x, int y, int chessCount, List<Chess> chesses) {

        for (int i = 0; i < chessCount; i++) {

            Chess c = chesses.get(i);

            if (c.getX() == x && c.getY() == y) {
                return true;
            }
        }

        return false;
    }


    //看是相同位置是否有棋子
    private static Chess getChess(int x, int y, int chessCount, List<Chess> chesses) {
        for (int i = 0; i < chessCount; i++) {
            Chess chess = chesses.get(i);
            if (chess.getX() == x && chess.getY() == y) {
                return chess;
            }
        }
        return null;
    }

    private static boolean Level(int x, int y, int chessCount, List<Chess> chesses, int WinCount, Chess chess) {
        //看向右是否练成一条线
        for (int i = x + 1; i <= x + 4 && i <= 13; i++) {
            //判断(i, y)有没有棋子，颜色是不是一样的
            Chess _chess = getChess(i, y, chessCount, chesses);//获取这个坐标的棋子对象
            if (_chess != null && _chess.getColor().equals(chess.getColor())) {
                WinCount++;
            } else {
                break;
            }
        }
        //看向左是否为一条直线
        for (int i = x - 1; i >= x - 4 && i >= 0; i--) {
            Chess _chess = getChess(i, y, chessCount, chesses);
            if (_chess != null && _chess.getColor().equals(chess.getColor())) {
                WinCount++;
            } else {
                break;
            }

        }

        return (WinCount >= 5);

    }

    private static boolean Upright(int x, int y, int chessCount, List<Chess> chesses, int WinCount, Chess chess) {
        //看向上是否为一条直线
        for (int i = y - 1; i >= y - 4 && i >= 0; i--) {
            Chess _chess = getChess(x, i, chessCount, chesses);
            if (_chess != null && _chess.getColor().equals(chess.getColor())) {
                WinCount++;
            } else {
                break;
            }
        }
        //看向下是否练成一条线
        for (int i = y + 1; i <= y + 4 && y <= 13; i++) {
            Chess _chess = getChess(x, i, chessCount, chesses);
            if (_chess != null && _chess.getColor().equals(chess.getColor())) {
                WinCount++;
            } else {
                break;
            }
        }
        return (WinCount >= 5);
    }

    //判断右斜是否在满足胜利条件是否在满足胜利条件
    private static boolean LfetAngle(int x, int y, int chessCount, List<Chess> chesses, int WinCount, Chess chess) {
        //左斜向下
        for (int i = x + 1, j = y - 1; i <= x + 4 && i <= 13 && j >= y - 4 && j >= 0; i++, j--) {
            Chess _chess = getChess(i, j, chessCount, chesses);
            if (_chess != null && _chess.getColor().equals(chess.getColor())) {
                WinCount++;
            } else {
                break;
            }
        }
        //左斜向上
        for (int i = y + 1, j = x - 1; i <= y + 4 && i <= 13 && j >= x - 4 && j >= 0; i++, j--) {
            Chess _chess = getChess(j, i, chessCount, chesses);
            if (_chess != null && _chess.getColor().equals(chess.getColor())) {
                WinCount++;
            } else {
                break;
            }
        }
        return (WinCount >= 5);
    }

    //判断右斜是否在满足胜利条件
    private static boolean rightAngle(int x, int y, int chessCount, List<Chess> chesses, int WinCount, Chess chess) {
        //右斜向上
        for (int i = x + 1, j = y + 1; i <= x + 4 && i <= 13 && j <= y + 4 && j <= 13; i++, j++) {
            Chess _chess = getChess(i, j, chessCount, chesses);
            if (_chess != null && _chess.getColor().equals(chess.getColor())) {
                WinCount++;
            } else {
                break;
            }
        }
        //右斜向下
        for (int i = x - 1, j = y - 1; i >= x - 4 && i >= 0 && j >= y - 4 && j >= 0; i--, j--) {
            Chess _chess = getChess(i, j, chessCount, chesses);
            if (_chess != null && _chess.getColor().equals(chess.getColor())) {
                WinCount++;
            } else {
                break;
            }
        }
        return (WinCount >= 5);
    }
}
